package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.auth.Role;
import com.zaimokuza.cn.entity.auth.UserRoleRelation;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.mapper.RoleMapper;
import com.zaimokuza.cn.service.ResourcesService;
import com.zaimokuza.cn.service.RoleService;
import com.zaimokuza.cn.util.AssertUtils;
import com.zaimokuza.cn.util.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zaimokuza
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private ResourcesService resourcesService;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public void addRole(Role role) {
        // 生成角色ID
        String uid = RandomUtils.get32Uid();
        role.setId(uid);
        // 校验名称不能重复
        Integer nameIsUse = roleMapper.queryNameIsUse(null, role.getName());
        AssertUtils.isNull(nameIsUse, CustomizeExceptionEnum.USER_NAME_IS_REPEAT);
        // 添加记录
        roleMapper.insertRecord(role);
    }

    @Override
    public void updateUserRoleRelations(UserRoleRelation userRoleRelation) {
        // 根据用户ID删除用户角色关联信息
        roleMapper.deleteBatchUserRoleRelationByUserId(userRoleRelation.getUserId());
        // 判断是否存在角色ID
        if (CollectionUtils.isEmpty(userRoleRelation.getRoleIdList())) {
            return;
        }
        // 批量添加记录
        roleMapper.insertBatchUserRoleRelation(userRoleRelation);
    }

    @Override
    public void updateRoleNameById(Role role) {
        // 校验名称不能重复
        Integer nameIsUse = roleMapper.queryNameIsUse(role.getId(), role.getName());
        AssertUtils.isNull(nameIsUse, CustomizeExceptionEnum.USER_NAME_IS_REPEAT);
        // 更新记录
        roleMapper.updateRecord(role);
    }

    @Override
    public void deleteRole(String id) {
        // 删除角色
        roleMapper.deleteRecord(id);
        // 删除用户角色关联信息
        roleMapper.deleteBatchUserRoleRelationByRoleId(id);
        // 删除角色资源关联信息
        resourcesService.deleteBatchRoleResourcesRelationByRoleId(id);
    }

    @Override
    public List<Role> getRoleTree() {
        return roleMapper.queryRolePage(null).getResult();
    }

    @Override
    public ArrayList<String> getRoleIdListByUserId(String userId) {
        return roleMapper.queryRoleIdListByUserId(userId);
    }

    @Override
    public PageResult<Role> getRolePage(String name, Integer page, Integer size) {
        // 设置分页信息
        PageHelper.startPage(page, size);
        // 查询
        Page<Role> resources = roleMapper.queryRolePage(name);
        // 返回结果
        return PageResult.from(resources);
    }

    @Override
    public ArrayList<Role> getRoleListByUserId(String userId) {
        return roleMapper.queryRoleListByUserId(userId);
    }

    @Override
    public ArrayList<Role> getRoleListByResourcesId(Long resourcesId) {
        return roleMapper.queryRoleListByResourcesId(resourcesId);
    }

}
